package com.yonyou.cyxdms.sysAuth.service.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.cyx.function.bean.dto.http.RestServiceResponseDto;
import com.yonyou.cyx.function.exception.ServiceBizException;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.constants.UserAuthConstant;
import com.yonyou.cyxdms.sysAuth.service.service.UserAuthService;
import com.yonyou.cyxdms.sysAuth.service.service.UserService;
import com.yonyou.cyxdms.sysAuth.service.service.feign.WuLinUserAuthClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: tanjie
 * @Description:
 * @Date: Create in 17:13 2019/7/23
 */

@Service(UserAuthConstant.SERVICE_NAME_USER_AUTH + UserAuthConstant.APP_SOURCE_WULIN)
@Transactional(rollbackFor = Exception.class)
public class WuLinUserAuthServiceImpl implements UserAuthService {

	// 日志对象
	private static final Logger logger = LoggerFactory.getLogger(WuLinUserAuthServiceImpl.class);

	@Autowired
	public UserService userService;
	@Value("${user-auth." + UserAuthConstant.APP_SOURCE_WULIN + ".auth-url}")
	private String wulinAuthUrl;
	@Autowired
	WuLinUserAuthClient wuLinUserAuthClient;

	@Override
	public LoginInfoDto getAppUserInfo(String token) {
		logger.debug("访问地址：{},token:{}", wulinAuthUrl, token);
//		return iUserPOService.getUserInfoByUserCode(UserAuthConstant.APP_SOURCE_WULIN, token);

        RestServiceResponseDto responseDto = wuLinUserAuthClient.getMainAppUserInfo(wulinAuthUrl, token);

        if (responseDto.getResultCode() == RestServiceResponseDto.SUCCESS) {
            // {
            // "data": [
            // {
            // "ROLE_ACCOUNT": "B51002701_SGMWGJ0014"
            // }
            // ],
            // "resultCode": "200",
            // "resultMsg": "查询成功"
            // }
            try {
                logger.debug("返回data:{}", responseDto.getResponseMsg());
                JSONObject jsonResponse = JSONObject.parseObject(responseDto.getResponseMsg());
                if (jsonResponse == null || jsonResponse.isEmpty() || !jsonResponse.containsKey("data")) {
                    throw new ServiceBizException(
                            jsonResponse != null && !jsonResponse.isEmpty() && jsonResponse.containsKey("resultMsg")
                                    ? jsonResponse.getString("resultMsg")
                                    : "认证接口请求异常");
                }
                JSONArray data = jsonResponse.getJSONArray("data");
                if (data == null || data.isEmpty()) {
                    throw new ServiceBizException("数据错误或此jwt不合法");
                }
                return userService.getUserInfoByUserCode(UserAuthConstant.APP_SOURCE_WULIN,
                        data.getJSONObject(0).getString("ROLE_ACCOUNT"));
            } catch (Exception e) {
                throw new ServiceBizException("数据错误或此jwt不合法");
            }
        } else {
            throw new ServiceBizException("此jwt 不合法");
        }
	}
}
